package com.woniuxy.propertyjava.controller;

import com.woniuxy.propertyjava.entity.Department;
import com.woniuxy.propertyjava.entity.ResponseResult;
import com.woniuxy.propertyjava.service.DepartmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DepartmentController {
    @Resource
    DepartmentService departmentService;
    //通过部门类型查询部门信息
    @GetMapping ("/departmenttype")
    public ResponseResult<Department> getDepartmentByDepartmenttype(String departmenttype){
        System.out.println("查询类型:"+departmenttype);
        List<Department> department = departmentService.getDepartmentByDepartmenttype(departmenttype);
        ResponseResult<Department> responseResult=new ResponseResult(200,"ok",department);
        return responseResult;
    }
    //查询部门
    @GetMapping("/department")
    public ResponseResult<Department> getAllDepartment(){
        System.out.println("查询部门");
        List<Department> allDepartment = departmentService.getAllDepartment();
        ResponseResult<Department> responseResult=new ResponseResult(200,"ok",allDepartment);
        return responseResult;
    }

    //添加部门
    @PostMapping("/department")
    public ResponseResult<Department> addDepartment(@RequestBody Department department){
        System.out.println("添加部门："+department);
        departmentService.insertDepartment(department);
        return new ResponseResult(200, "ok", null);
    }

    //修改部门信息
    @PutMapping("/department")
    public ResponseResult<Department> modifyDepartment(@RequestBody Department department){
        System.out.println("修改部门");
        departmentService.updateDepartment(department);
        return new ResponseResult(200, "ok", null);
    }

    @DeleteMapping("/department/{departmentid}")
    public ResponseResult<Department> removeDepartment(@PathVariable("departmentid") int departmentid){
        System.out.println("删除部门："+departmentid);
        departmentService.deletDepartmentByDepartmentid(departmentid);
        return new ResponseResult(200, "ok", null);
    }
}
